<?php

function print_db_result($result)
{
	print("<table><caption>Result from DB</caption><thead><tr>");
	foreach ($result as $key => $value)
		print('<th>'.$key.'</th>');
	print("</tr></thead><tbody><tr>");
	foreach ($result as $key => $value)
		print('<td>'.$value.'</td>');
	print("</tr>");
}

function init_connect_db()
{
	$db = mysql_connect('localhost', 'group5', '6unHehKP');
	if (!$db) {
    	print('Could not connect: '.mysql_error());
    	exit();
    }
	if (!mysql_select_db("group5")) {
		printf('Could not open: '.'group5 table');
		exit();
	}
	return $db;
}

function get_title_places_by_user_id($id)
{
	$temp = "SELECT id, title, dateEdit FROM Places WHERE Users_id = '".addslashes($id)."'";
	$query = mysql_query($temp);
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	//$Query_result = mysql_fetch_assoc($query);
	return $query;
}

function get_title_comments_by_user_id($id)
{
	$temp = "SELECT *
			 FROM Places AS places, Comments AS comm
			 WHERE comm.Places_id = places.id
			 AND comm.Users_id = $id";
			 
//print($temp);
	//'".addslashes($id)."'
	/*'SELECT *
FROM post AS p, category AS c
WHERE p.category_id = c.category_id;'*/
	
	$query = mysql_query($temp);
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	//$Query_result = mysql_fetch_assoc($query);
	//print_db_result($Query_result);
	return $query;
}

function get_title_dos_donts()
{
	$query = mysql_query("SELECT title,type,id FROM Tips");
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}

	return $query;
}

function get_dos_donts_from_id($id)
{
	$temp = "SELECT * FROM Tips WHERE id = '".addslashes($id)."'";
	$query = mysql_query($temp);
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}

	return $query;
}

function get_dos_donts()
{
	$query = mysql_query("SELECT * FROM Tips");
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	return $query;
}

function get_dos_donts_city($type)
{
	$query = mysql_query("SELECT DISTINCT city FROM Tips WHERE type = '".addslashes($type)."'");
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	return $query;
}

function get_dos_donts_city_type($city, $type)
{
	$query = mysql_query("SELECT * FROM Tips WHERE city = '".addslashes($city)."' AND type = '".addslashes($type)."'");
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	return $query;
}

function get_title_places()
{
	$query = mysql_query("SELECT title,id FROM Places");
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	//$Query_result = mysql_fetch_assoc($query);
	//print_db_result($Query_result);
	return $query;
}

function get_content_places_from_id($id)
{
	$temp = "SELECT * FROM Places WHERE id = '".addslashes($id)."'";
	$query = mysql_query($temp);
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	//$Query_result = mysql_fetch_assoc($query);
	//print_db_result($Query_result);	
	return $query;
}

function get_comments_from_place($place)
{
	$temp = "SELECT * FROM Comments WHERE Places_id = '".addslashes($place)."'";
	$query = mysql_query($temp);
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	//$Query_result = mysql_fetch_assoc($query);
	//print_db_result($Query_result);	
	return $query;
}

function get_faqs()
{
	$query = mysql_query("SELECT question, id FROM Faqs");
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	return $query;
}

function get_answer_faq_from_id($question_id)
{
	$temp = "SELECT * FROM Faqs WHERE id = '".addslashes($question_id)."'";
	$query = mysql_query($temp);
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	return $query;
}

function get_info_from_user($user_id)
{
	$query = mysql_query("SELECT * FROM Users WHERE id = $user_id");
	//print($query);
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	$Query_result = mysql_fetch_assoc($query);
	return $Query_result;
}

function get_all_users_info()
{
	$query = mysql_query("SELECT * FROM Users");
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	$Query_result = mysql_fetch_assoc($query);
	//print_db_result($Query_result);
	return $Query_result;
}

function isARegisteredUser($login, $pass)
{
	$temp = "SELECT * FROM Users WHERE email = \"$login\"";
	print("Temporary request : ".$temp);
	$query = mysql_query($temp);
	if (!$query) {
		printf('Could not execut the query');
		exit();
	}
	$Query_result = mysql_fetch_assoc($query);
	print_db_result($Query_result);	
}

function add_user($lastname, $firstname, $email, $password, $dateofbirth, $admin, $imgUrl)
{
	$dateCreate = date("Y-m-d H:i:s");
	$temp = "INSERT INTO Users (id, lastName, firstName, email, password, dateofbirth, admin, imageUrl, dateCreate) VALUES (NULL, '$lastname', '$firstname', '$email', '$password', '$dateofbirth', '$admin', '$imgUrl', '$dateCreate')";
	print("Temporary request : ".$temp);
	$query = mysql_query($temp);
	if (!$query){
		printf('Could not execut the query');
		exit();
	}
	print($query);
}

function modify_user($lastname, $firstname, $email, $password, $dateofbirth, $admin, $imgUrl)
{
	$temp = "UPDATE Users SET lastName='$lastname', firstName='$firstname', email='$email', password='$password', dateofbirth='$dateofbirth', admin='$admin', imageUrl='$imgUrl' WHERE email='$email'";
	print("Temporary request : ".$temp);
	$query = mysql_query($temp);
	if (!$query){
		printf('Could not execut the query');
		exit();
	}
	print($query);
}

function add_dos_and_donts($title, $type, $article, $city, $userId)
{
	$dateCreate = date("Y-m-d H:i:s");
	$temp = "INSERT INTO Tips (id, title, type, article, city, Users_id, dateEdit, dateCreate) VALUES (NULL, '$title', '$type', '$article', '$city', '$userId', '$dateCreate', '$dateCreate')";
	print("Temporary request : ".$temp);
	$query = mysql_query($temp);
	if (!$query){
		printf('Could not execut the query');
		exit();
	}
	print($query);
}

function add_places_to_visit($title, $article, $userId)
{
	$dateCreate = date("Y-m-d H:i:s");
	$temp = "INSERT INTO Places (id, title, article, dateEdit, dateCreate, Users_id) VALUES (NULL, '$title', '$article', '$dateCreate', '$dateCreate', '$userId')";
	print("Temporary request : ".$temp);
	$query = mysql_query($temp);
	if (!$query){
		printf('Could not execut the query');
		exit();
	}
	print($query);
}

function modify_dos_and_donts($title, $type, $article, $city, $userId, $idDosDonts)
{
	$dateModification = date("Y-m-d H:i:s");
	$temp = "UPDATE Users SET title='$title', type='$type', article='$article', city='$city', Users_id='$userId' dateEdit='$dateModification' WHERE id='$idDosDonts'";
	print("Temporary request : ".$temp);
	$query = mysql_query($temp);
	if (!$query){
		printf('Could not execut the query');
		exit();
	}
	print($query);
}

function modify_places_to_visit($title, $article, $userId, $idPlaces)
{
	$dateModification = date("Y-m-d H:i:s");
	$temp = "UPDATE Users SET title='$title', article='$article', dateEdit='$dateModification' WHERE id='$idPlaces'";
	print("Temporary request : ".$temp);
	$query = mysql_query($temp);
	if (!$query){
		printf('Could not execut the query');
		exit();
	}
	print($query);
}

function remove_articles_from_user_id($idPlace)
{
	$temp = "DELETE FROM `Places` WHERE `Places`.`id` = $idPlace LIMIT 1";
	print("Temporary request : ".$temp);
	$query = mysql_query($temp);
	if (!$query){
		printf('Could not execut the query');
		exit();
	}
	print($query);
}

function remove_comments_from_user_id($idComment)
{
	$temp = "DELETE FROM `Comments` WHERE `Comments`.`id` = $idComment LIMIT 1";
	print("Temporary request : ".$temp);
	$query = mysql_query($temp);
	if (!$query){
		printf('Could not execut the query');
		exit();
	}
	print($query);
}


// OPEN
$myDataBase = init_connect_db();

// TEST METHODS
// get_dos_donts();

// CLOSE
mysql_close($myDataBase);

?>